package com.caochenlei.graph;

import java.util.LinkedList;
import java.util.Queue;

public class EdgeWeightedDigraph {
    private int N;                      //表示顶点数目
    private int E;                      //表示边的数目
    private Queue<DirectedEdge>[] adj;  //表示邻接表

    public EdgeWeightedDigraph(int n) {
        this.N = n;                     //初始化顶点数目
        this.E = 0;                     //初始化边的数目
        this.adj = new LinkedList[n];   //初始化邻接表

        for (int i = 0; i < adj.length; i++) {
            adj[i] = new LinkedList<>();
        }
    }

    //获取顶点数目
    public int size() {
        return N;
    }

    //获取边的数目
    public int edge() {
        return E;
    }

    //添加一条边
    public void addEdge(DirectedEdge e) {
        int v = e.from();
        adj[v].add(e);
        E++;
    }

    //删除一条边
    public void removeEdge(DirectedEdge e) {
        int v = e.from();
        adj[v].remove(e);
        E--;
    }

    //获取顶点v指出的所有的边
    public Queue<DirectedEdge> adj(int v) {
        return adj[v];
    }

    //获取加权有向图的所有边
    public Queue<DirectedEdge> edges() {
        Queue<DirectedEdge> allEdges = new LinkedList<>();
        for (int v = 0; v < N; v++) {
            for (DirectedEdge e : adj[v]) {
                allEdges.add(e);
            }
        }
        return allEdges;
    }
}
